﻿using System;

namespace Outfish {

	/// <summary>
	/// Creates a selector where caller supplies the source string and the predicate
	/// </summary>
	internal class PredicateSelector : INodeSelector {

		#region constructor

		public PredicateSelector( Predicate<HtmlNode> pred, string sourceString ){
			this._pred = pred;
			this._s = sourceString;
 		}
	
		#endregion
	
		public bool IsMatch(HtmlNode node){
			return _pred( node );
		}
		
		public override string ToString() { return this._s; }

		#region private fields
		
		string _s;
		Predicate<HtmlNode> _pred;
		
		#endregion

		static public bool IsFirstChild( HtmlNode node ){ return node.SiblingIndex == 0; }
		static public bool IsLastChild ( HtmlNode node ){ return node.SiblingIndex == node.SiblingCount-1; }
		static public bool IsOnlyChild ( HtmlNode node ){ return node.SiblingCount == 1; }
		static public bool IsParent    ( HtmlNode node ){ return node.IsParent; }
		static public bool IsEmpty     ( HtmlNode node ){ return node.IsEmpty; }
		
	}
	
}
